// pages/operation/operation.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    pageHeight: app.globalData.pageHeight,
    userInfo: {},
    callPhoneDialog: false,
    phoneNumber: '',
    parkingList: [],
    parkManagerList: [],
    currentPage: 1,
    pageSize: 10,
    canIUseGetUserProfile: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = wx.getStorageSync('lmtc_admin_shopInfo')
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.setData({
      userInfo: {
        avatarUrl: data.avatar,
        nickName: data.nickName,
        phone: data.telephone,
        isOwner: data.isOwner
      }
    })
  },
  onShow() {
    // this.getParkManagerList()
    this.getParkingList()
  },
  /**
   * 退出登录
   */
  logout() {
    wx.showModal({
      title: '操作确认',
      content: '是否确认退出登录？',
      confirmText: '确认退出',
      success(res) {
        if (res.confirm) {
          wx.reLaunch({
            url: '/pages/login/login',
          })
          wx.removeStorageSync('lmtc_admin_token')
          wx.removeStorageSync('lmtc_admin_shopInfo')
        }
      }
    })
  },
  /**
   * 获取用户信息
   */
  getUserInfo(e) {
    let that = this
    let userInfo = e.detail.userInfo
    wx.login({
      success(res) {
        let parmas = {
          avatar: userInfo.avatarUrl,
          nickname: userInfo.nickName,
          sex: userInfo.gender == 1 ? 'MEN' : 'WOMEN',
          code: res.code
        }
        request.http('/shop/shop-manager/authorize', 'POST', parmas)
          .then(res => {
            let data = res.data
            if (data.code == 200) {
              that.setData({
                userInfo: {
                  avatarUrl: data.data.avatar,
                  nickName: data.data.nickname,
                  phone: data.data.telephone
                }
              })
              let parkInfo = wx.getStorageSync('lmtc_admin_shopInfo')
              parkInfo['avatar'] = data.data.avatar
              parkInfo['nickName'] = data.data.nickName
              parkInfo['mobilePhone'] = data.data.telephone
              wx.setStorageSync('lmtc_admin_shopInfo', parkInfo)
            }
          })
      }
    })
  },
  /**
 * 获取用户信息
 */
  getUserProfile(e) {
    let that = this;
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        let userInfo = res.userInfo
        console.log(userInfo);
        wx.login({
          success(res) {
            let parmas = {
              avatar: userInfo.avatarUrl,
              nickname: userInfo.nickName,
              sex: userInfo.gender == 1 ? 'MEN' : 'WOMEN',
              code: res.code
            }
            request.http('/shop/shop-manager/authorize', 'POST', parmas)
              .then(res => {
                let data = res.data
                if (data.code == 200) {
                  that.setData({
                    userInfo: {
                      avatarUrl: data.data.avatar,
                      nickName: data.data.nickname,
                      phone: data.data.telephone
                    }
                  })
                  let parkInfo = wx.getStorageSync('lmtc_admin_shopInfo') ? wx.getStorageSync('lmtc_admin_shopInfo') : {}
                  parkInfo['avatar'] = data.data.avatar
                  parkInfo['nickName'] = data.data.nickname
                  parkInfo['telephone'] = data.data.telephone
                  wx.setStorageSync('lmtc_admin_shopInfo', parkInfo)
                }
              })
          }
        })
      }
    })
  },
  /**
   * 获取商户列表
   */
  getParkingList() {
    let that = this
    request.http('/shop/shop-info/get-shop-info-list', 'GET', { manageId: wx.getStorageSync('lmtc_shop_userId') })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            parkingList: data.data
          })
        }
      })
  },
  /**
   * 获取车场管理员列表
   */
  // getParkManagerList() {
  //   let that = this
  //   let parmas = {
  //     currentPage: this.data.currentPage,
  //     size: this.data.pageSize
  //   }
  //   request.http('/park/park-manager/page', 'GET', parmas)
  //     .then(res => {
  //       let data = res.data
  //       if (data.code == 200) {
  //         that.setData({
  //           parkManagerList: data.data.records
  //         })
  //       }
  //     })
  // },
  /**
   * 打开拨号弹窗
   */
  showCallPhoneDialog(e) {
    this.setData({
      callPhoneDialog: true,
      phoneNumber: e.target.dataset.phone
    })
  },
  /**
   * 关闭拨号弹窗
   */
  hiddenCallPhoneDialog() {
    this.setData({
      callPhoneDialog: false
    })
  },
  /**
   * 拨打电话
   */
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.phoneNumber,
    })
    this.hiddenCallPhoneDialog()
  },
  // 删除管理员
  delManager(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '操作确认',
      content: '是否确认删除管理员？',
      success(res) {
        if (res.confirm) {
          request.http('/shop/shop-manager/delete', 'POST', {id})
            .then(res => {
              let data = res.data
              if (data.code == 200) {
                wx.showToast({
                  title: '删除成功'
                })
                that.getParkingList()
              }
            })
        }
      }
    })
  },
  // 跳转页面
  toPage(e) {
    let url = e.target.dataset.url
    wx.navigateTo({
      url: url,
    })
  }
})